package com.edu.oracle.controller;

import com.edu.oracle.pojo.Score;
import com.edu.oracle.pojo.vo.ScoreVo;
import com.edu.oracle.service.ScoreService;
import com.edu.oracle.service.impl.ScoreServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ScoreController extends HttpServlet {

    //new一个类，是不是需要往前面移，注入
    ScoreService scoreService = new ScoreServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);//所有的post请求交给get请求处理
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求路径
        String requestURI = req.getRequestURI();
        System.out.println("请求路径："+requestURI);

        //         数请求路径有多长/studentWebSystem_war_exploded/
        requestURI= requestURI.substring(31);//截取后再赋值给自己
        System.out.println("url:"+requestURI);
        //你们学习后，再提升自己。螺旋上升

        //2.判断是哪一种请求
        if(requestURI.equals("listScore")) {

            //1.获取数据

            //2.调用service业务层方法
            List<ScoreVo> scoreList = scoreService.list();//类名称必须用大写开头，方法是小写开头的

            //3.返回结果
            req.setAttribute("scoreList",scoreList);

            //forward转发到listScore.jsp页面，不是servlet
            req.getRequestDispatcher("listScore.jsp").forward(req,resp);


        }else if(requestURI.equals("deleteScore")){

            //1.获取数据
            String sno = req.getParameter("sno");
            System.out.println("score表的删除请求:"+sno);

            //2.调用service层的删除方法？？？

            //3. 处理结果
            //转发到查询所以成绩的servlet，而不是页面
            req.getRequestDispatcher("listScore").forward(req,resp);
        }else if(requestURI.equals("viewScore")){//更新查询功能
            //1.
            String sno = req.getParameter("sno");
            System.out.println("更新sno查询:"+sno);

            String cno = req.getParameter("cno");
            System.out.println("更新cno查询:"+cno);

            //2.
            ScoreVo scoreVo = scoreService.selectByOne(sno,cno);

            //3.
            //3.处理结果
            if (scoreVo!=null){//查询成功，转发到更新页面
                req.setAttribute("scoreVo",scoreVo);
                req.getRequestDispatcher("updateScore.jsp").forward(req,resp);
            }else{//失败，请求查询所有servlet路径，实现查询所用和转发到查询所用列表
                req.getRequestDispatcher("listScore").forward(req,resp);
            }
        }else if(requestURI.equals("updateScore")){
            //1.获取参数
            Score score = new Score();
            score.setCno(req.getParameter("cno"));
            score.setSno(req.getParameter("sno"));
            score.setGrade(Float.parseFloat(req.getParameter("grade")));

            //2.调用         dao持久层  插入insert into score values (?,?,?);  和course表操作一样
        }

    }
}
